﻿Imports Tom0014
Imports Tom0014TableAdapters
Imports System.Data.SqlClient
Imports System.IO
Partial Class Registro_Fotos
    Inherits System.Web.UI.Page
    Dim animales As New animalTableAdapter
    Dim animalPadre As New animalTableAdapter
    Dim animalMadre As New animalTableAdapter
    Dim sSQL As String
    Dim sdsFotos As New SqlDataSource
    Dim iAnimalId As Integer
    Protected Sub Registro_Fotos_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            iAnimalId = CType(Request.QueryString("Animal"), Integer)
        Catch ex As Exception
            iAnimalId = 0
        End Try
        Dim iPadre As Integer
        Dim iMadre As Integer
        Dim animal = animales.GetAnimalById(iAnimalId)
        If animal.Count > 0 Then
            lblAnimal.Text = animal(0).claveSNLA + " " + animal(0).nombre
            txtAnimal.Text = animal(0).claveSNLA
            TxtNombreAnimal.Text = animal(0).nombre
            iPadre = animal(0).id_padre
            iMadre = animal(0).id_madre
        End If
        Dim animal2 = animalPadre.GetAnimalById(iPadre)
        If animal2.Count > 0 Then
            txtPadre.Text = animal2(0).claveSNLA
            TxtNombrePadre.Text = animal2(0).nombre
        End If

        Dim animal3 = animalMadre.GetAnimalById(iMadre)
        If animal3.Count > 0 Then
            txtMadre.Text = animal3(0).claveSNLA
            TxtNombraMadre.Text = animal3(0).nombre
        End If
        BorraImg()
        iniciaRadioB()
        consultaImg(iAnimalId)
    End Sub
    Sub InstTabla(ByRef numero As Integer, ByVal nombre As String)
        Dim fecha As String = Date.Today
        Dim principal As String = "0"
        Dim iNumber As Integer = iAnimalId
        sSQL = "INSERT INTO Fotos (AnimalId, NoFoto, NombreArchivo, FechaAlta, FotoPrincipal) VALUES  (" + iNumber.ToString + "," + numero.ToString + ", '" + nombre.ToString + "'" + " ,'" + Date.Now.ToShortDateString + "','" + principal.ToString + "'); " & _
        "UPDATE animal set photo = 1 WHERE AnimalId=" & iNumber.ToString & "; "
        Try
            sdsFotos.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            sdsFotos.InsertCommand = sSQL
            sdsFotos.Insert()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
    Sub mensajeError(ByVal numero As String, ByVal mensaje As String)
        If numero = 1 Then
            LblError1.Visible = True
            LbLMensaje1.Visible = False
            LblError1.Text = mensaje
        End If
        If numero = 2 Then
            LblError2.Visible = True
            LbLMensaje2.Visible = False
            LblError2.Text = mensaje
        End If
        If numero = 3 Then
            LblError3.Visible = True
            LbLMensaje3.Visible = False
            LblError3.Text = mensaje
        End If
        If numero = 4 Then
            LblError4.Visible = True
            LbLMensaje4.Visible = False
            LblError4.Text = mensaje
        End If
        If numero = 5 Then
            LblError5.Visible = True
            LbLMensaje5.Visible = False
            LblError5.Text = mensaje
        End If
        If numero = 6 Then
            LblError6.Visible = True
            LbLMensaje6.Visible = False
            LblError6.Text = mensaje
        End If
    End Sub
    Sub mensajeClave(ByVal numero As String, ByVal mensaje As String)
        If numero = 1 Then
            LblError1.Visible = False
            LbLMensaje1.Visible = True
            LbLMensaje1.Text = mensaje
        End If
        If numero = 2 Then
            LblError2.Visible = False
            LbLMensaje2.Visible = True
            LbLMensaje2.Text = mensaje
        End If
        If numero = 3 Then
            LblError3.Visible = False
            LbLMensaje3.Visible = True
            LbLMensaje3.Text = mensaje
        End If
        If numero = 4 Then
            LblError4.Visible = False
            LbLMensaje4.Visible = True
            LbLMensaje4.Text = mensaje
        End If
        If numero = 5 Then
            LblError5.Visible = False
            LbLMensaje5.Visible = True
            LbLMensaje5.Text = mensaje
        End If
        If numero = 6 Then
            LblError6.Visible = False
            LbLMensaje6.Visible = True
            LbLMensaje6.Text = mensaje
        End If
    End Sub
    Sub BorraImg()
        Image1.ImageUrl = "~/photos/toro.jpg"
        Image2.ImageUrl = "~/photos/toro.jpg"
        Image3.ImageUrl = "~/photos/toro.jpg"
        Image4.ImageUrl = "~/photos/toro.jpg"
        Image5.ImageUrl = "~/photos/toro.jpg"
        Image6.ImageUrl = "~/photos/toro.jpg"
        LbLMensaje1.Text = ""
        LbLMensaje2.Text = ""
        LbLMensaje3.Text = ""
        LbLMensaje4.Text = ""
        LbLMensaje5.Text = ""
        LbLMensaje6.Text = ""
        LblError1.Text = ""
        LblError2.Text = ""
        LblError3.Text = ""
        LblError4.Text = ""
        LblError5.Text = ""
        LblError6.Text = ""
    End Sub
    Sub consultaImg(ByVal id As Integer)
        Dim Numero As Integer
        Dim Fotos As String
        Dim principal As Boolean
        Dim ConexionBD As New SqlConnection()
        ConexionBD.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
        Dim OrdenSQL As SqlCommand = Nothing
        Dim Lector As SqlDataReader = Nothing
        Try
            Dim Consulta As String = "Select AnimalId, NoFoto, NombreArchivo, FechaAlta, FotoPrincipal from Fotos where AnimalId=" & id
            OrdenSQL = New SqlCommand(Consulta, ConexionBD)
            ConexionBD.Open()
            Lector = OrdenSQL.ExecuteReader()
            Dim salida As Integer
            While salida = 0
                If Lector.Read() Then
                    Numero = Lector("NoFoto")
                    Fotos = Lector("NombreArchivo")
                    principal = Lector("FotoPrincipal")
                    If Numero = "1" Then
                        Image1.ImageUrl = "~/photos/Galeria/" + Fotos
                        LbLMensaje1.Text = Fotos
                        LbLMensaje1.Visible = True
                        RadioButton1.Visible = True
                        RadioButton1.Checked = principal
                    End If
                    If Numero = "2" Then
                        Image2.ImageUrl = "~/photos/Galeria/" + Fotos
                        LbLMensaje2.Text = Fotos
                        LbLMensaje2.Visible = True
                        RadioButton2.Visible = True
                        RadioButton2.Checked = principal
                    End If
                    If Numero = "3" Then
                        Image3.ImageUrl = "~/photos/Galeria/" + Fotos
                        LbLMensaje3.Text = Fotos
                        LbLMensaje3.Visible = True
                        RadioButton3.Visible = True
                        RadioButton3.Checked = principal
                    End If
                    If Numero = "4" Then
                        Image4.ImageUrl = "~/photos/Galeria/" + Fotos
                        LbLMensaje4.Text = Fotos
                        LbLMensaje4.Visible = True
                        RadioButton4.Visible = True
                        RadioButton4.Checked = principal
                    End If
                    If Numero = "5" Then
                        Image5.ImageUrl = "~/photos/Galeria/" + Fotos
                        LbLMensaje5.Text = Fotos
                        LbLMensaje5.Visible = True
                        RadioButton5.Visible = True
                        RadioButton5.Checked = principal
                    End If
                    If Numero = "6" Then
                        Image6.ImageUrl = "~/photos/Galeria/" + Fotos
                        LbLMensaje6.Text = Fotos
                        LbLMensaje6.Visible = True
                        RadioButton6.Visible = True
                        RadioButton6.Checked = principal
                    End If
                Else
                    salida = 1
                End If

            End While
        Catch ex As Exception

        End Try
    End Sub
    Sub BorraFotoTablaYFisico(ByVal NombreFoto As String)
        Dim numeroFoto As Integer
        Dim lenFoto As Integer
        Dim codigo As String

        lenFoto = Len(NombreFoto)
        For I = 0 To lenFoto
            If I < lenFoto Then
                codigo = NombreFoto.Substring(I, 1)
                If codigo = "." Then
                    numeroFoto = CInt(NombreFoto.Substring(I - 1, 1))
                    ID = CInt(NombreFoto.Substring(0, I - 2))
                End If
            End If
        Next I
        Try
            iAnimalId = CType(Request.QueryString("Animal"), Integer)
        Catch ex As Exception
            iAnimalId = 0
        End Try

        Dim iNumber As Integer = iAnimalId

        If numeroFoto = 1 Then
            sSQL = "DELETE FROM Fotos where AnimalId = " & ID & " and " & "NoFoto = " & numeroFoto & ";" & _
            "UPDATE animal set photo = 0 WHERE AnimalId=" & iNumber.ToString & ";"
        Else
            sSQL = "DELETE FROM Fotos where AnimalId = " & ID & " and " & "NoFoto = " & numeroFoto
        End If
        Try
            sdsFotos.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            sdsFotos.DeleteCommand = sSQL
            sdsFotos.Delete()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try

        Dim archi As String = "NombreFoto"
        Try
            Dim FileToDelete As String
            'Establecer la ruta completa al archivo 
            FileToDelete = Server.MapPath("~/photos/Galeria/" + NombreFoto)
            ' Eliminar un archivo 
            File.Delete(FileToDelete)
        Catch ex As Exception

        End Try
    End Sub
    Protected Sub BtnBorraFoto1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnBorraFoto1.Click
        Dim NombreFoto As String
        NombreFoto = LbLMensaje1.Text
        If Len(NombreFoto) = 0 Then
            Return
        End If
        BorraFotoTablaYFisico(NombreFoto)
        Image1.ImageUrl = "~/photos/toro.jpg"
        LbLMensaje1.Text = ""
        LblError1.Text = ""
        RadioButton1.Visible = False
    End Sub

    Protected Sub BtnBorraFoto2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnBorraFoto2.Click
        Dim NombreFoto As String
        NombreFoto = LbLMensaje2.Text
        If Len(NombreFoto) = 0 Then
            Return
        End If
        BorraFotoTablaYFisico(NombreFoto)
        Image2.ImageUrl = "~/photos/toro.jpg"
        LbLMensaje2.Text = ""
        LblError2.Text = ""
        RadioButton2.Visible = False
    End Sub

    Protected Sub BtnBorraFoto3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnBorraFoto3.Click
        Dim NombreFoto As String
        NombreFoto = LbLMensaje3.Text
        If Len(NombreFoto) = 0 Then
            Return
        End If
        BorraFotoTablaYFisico(NombreFoto)
        Image3.ImageUrl = "~/photos/toro.jpg"
        LbLMensaje3.Text = ""
        LblError3.Text = ""
        RadioButton3.Visible = False
    End Sub

    Protected Sub BtnBorraFoto4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnBorraFoto4.Click
        Dim NombreFoto As String
        NombreFoto = LbLMensaje4.Text
        If Len(NombreFoto) = 0 Then
            Return
        End If
        BorraFotoTablaYFisico(NombreFoto)
        Image4.ImageUrl = "~/photos/toro.jpg"
        LbLMensaje4.Text = ""
        LblError4.Text = ""
        RadioButton4.Visible = False
    End Sub

    Protected Sub BtnBorraFoto5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnBorraFoto5.Click
        Dim NombreFoto As String
        NombreFoto = LbLMensaje5.Text
        If Len(NombreFoto) = 0 Then
            Return
        End If
        BorraFotoTablaYFisico(NombreFoto)
        Image5.ImageUrl = "~/photos/toro.jpg"
        LbLMensaje5.Text = ""
        LblError5.Text = ""
        RadioButton5.Visible = False
    End Sub

    Protected Sub BtnBorraFoto6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnBorraFoto6.Click
        Dim NombreFoto As String
        NombreFoto = LbLMensaje6.Text
        If Len(NombreFoto) = 0 Then
            Return
        End If
        BorraFotoTablaYFisico(NombreFoto)
        Image6.ImageUrl = "~/photos/toro.jpg"
        LbLMensaje6.Text = ""
        LblError6.Text = ""
        RadioButton6.Visible = False
    End Sub

    Protected Sub RadioButton1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        Dim seleccionado As Integer = 1
        Dim control As RadioButton = RadioButton1
        RadioSeleccionado(seleccionado, iAnimalId, control)
    End Sub

    Protected Sub RadioButton2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        Dim seleccionado As Integer = 2
        Dim control As RadioButton = RadioButton2
        RadioSeleccionado(seleccionado, iAnimalId, control)
    End Sub

    Protected Sub RadioButton3_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
        Dim seleccionado As Integer = 3
        Dim control As RadioButton = RadioButton3
        RadioSeleccionado(seleccionado, iAnimalId, control)
    End Sub

    Protected Sub RadioButton4_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
        Dim seleccionado As Integer = 4
        Dim control As RadioButton = RadioButton4
        RadioSeleccionado(seleccionado, iAnimalId, control)
    End Sub

    Protected Sub RadioButton5_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged
        Dim seleccionado As Integer = 5
        Dim control As RadioButton = RadioButton5
        RadioSeleccionado(seleccionado, iAnimalId, control)
    End Sub

    Protected Sub RadioButton6_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton6.CheckedChanged
        Dim seleccionado As Integer = 6
        Dim control As RadioButton = RadioButton6
        RadioSeleccionado(seleccionado, iAnimalId, control)
    End Sub
    Sub RadioSeleccionado(ByVal numero As Integer, ByVal id As Integer, ByRef control As RadioButton)
        Dim numeroFoto As Integer
        Dim actina As String
        Dim ConexionBD As New SqlConnection()
        ConexionBD.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
        Dim OrdenSQL As SqlCommand = Nothing
        Dim Lector As SqlDataReader = Nothing
        Try
            Dim Consulta As String = "Select AnimalId, NoFoto, NombreArchivo, FechaAlta, FotoPrincipal from Fotos where AnimalId=" & id
            OrdenSQL = New SqlCommand(Consulta, ConexionBD)
            ConexionBD.Open()
            Lector = OrdenSQL.ExecuteReader()
            Dim salida As Integer
            While salida = 0
                If Lector.Read() Then
                    numeroFoto = Lector("NoFoto")
                    If numero = numeroFoto Then
                        actina = "1"
                        control.Checked = True
                    Else
                        actina = "0"
                        'control.Checked = False
                    End If
                    sSQL = "UPDATE Fotos set FotoPrincipal = " & actina & " WHERE AnimalId=" & id.ToString & " and NoFoto=" & numeroFoto & ";"
                    Try
                        sdsFotos.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
                        sdsFotos.UpdateCommand = sSQL
                        sdsFotos.Update()
                    Catch ex As Exception
                        Response.Write(ex.Message)
                    End Try

                Else
                    salida = 1
                End If



            End While
        Catch ex As Exception

        End Try
    End Sub
    Sub iniciaRadioB()
        RadioButton1.Checked = False
        RadioButton1.Visible = False
        RadioButton2.Checked = False
        RadioButton2.Visible = False
        RadioButton3.Checked = False
        RadioButton3.Visible = False
        RadioButton4.Checked = False
        RadioButton4.Visible = False
        RadioButton5.Checked = False
        RadioButton5.Visible = False
        RadioButton6.Checked = False
        RadioButton6.Visible = False
    End Sub
End Class
